package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.MyOngoingActivityListsBean;
import com.runen.infinitemovement.model.enums.ActivityStatus;
import com.runen.infinitemovement.model.enums.ColorType;
import com.runen.infinitemovement.model.enums.SportType;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MyHomeBannerAdapter extends BannerAdapter<MyOngoingActivityListsBean, MyHomeBannerAdapter.BannerViewHolder1> {
    private Context context;

    public static final int OBJECTIVE_CHALLENGES = 2;
    public static final int GOOD_ATTENDANCE_CLOCK = 3;
    public static final int TEAM_COMPETE = 4;

    public MyHomeBannerAdapter(Context context, List<MyOngoingActivityListsBean> datas) {
        super(datas);
        this.context = context;
    }

    @Override
    public MyHomeBannerAdapter.BannerViewHolder1 onCreateHolder(ViewGroup parent, int viewType) {
        View view;
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        switch (viewType) {
//            case OBJECTIVE_CHALLENGES:
//                view = LayoutInflater.from(context).inflate(R.layout.item_activity_objective, null);
//                view.setLayoutParams(lp);
//                BannerViewHolder1 bannerViewHolder1 = new BannerViewHolder1(view);
//                return bannerViewHolder1;
//            case GOOD_ATTENDANCE_CLOCK:
//            case TEAM_COMPETE:
//                view = LayoutInflater.from(context).inflate(R.layout.item_activity_other, null);
//                view.setLayoutParams(lp);
//                BannerViewHolder2 bannerViewHolder2 = new BannerViewHolder2(view);
//                return bannerViewHolder2;
//            default:
//                view = LayoutInflater.from(context).inflate(R.layout.item_activity_objective, null);
//                view.setLayoutParams(lp);
//                BannerViewHolder1 bannerViewHolder3 = new BannerViewHolder1(view);
//                return bannerViewHolder3;
//        }
        view = LayoutInflater.from(context).inflate(R.layout.item_activity_objective, null);
        view.setLayoutParams(lp);
        BannerViewHolder1 bannerViewHolder1 = new BannerViewHolder1(view);
        return bannerViewHolder1;
    }

    @Override
    public void onBindView(MyHomeBannerAdapter.BannerViewHolder1 holder, MyOngoingActivityListsBean data, int position, int size) {
        if (holder == null) return;
        if (holder instanceof BannerViewHolder1) {
            String time = "%s-%s";
            ((BannerViewHolder1) holder).tv_time.setText(String.format(time, data.beginTime, data.deadlineTime));
            setStatus(((BannerViewHolder1) holder).tv_status, data.activityStatus);
            ((BannerViewHolder1) holder).tv_user_nick.setText(data.publisherName);
            ((BannerViewHolder1) holder).tv_tenant_name.setText("-" + data.tenantName);
            GlideUtils.loadImage(context, data.publisherheadImgUrl, ((BannerViewHolder1) holder).rci_user_header);

            if (data.activityType == TEAM_COMPETE) {
                holder.ll_company.setVisibility(View.VISIBLE);
                holder.ll_objective.setVisibility(View.GONE);
                if (data.teamActivitiesDtoList != null && data.teamActivitiesDtoList.size() > 0) {
                    for (MyOngoingActivityListsBean.TeamActivitiesDtoListBean bean : data.teamActivitiesDtoList) {
                        switch (bean.colourType) {
                            case ColorType.COLOR_BLUE:
                                holder.tv_right_name.setText(bean.teamName);
                                holder.tv_right_people_num.setText(bean.participantsNumber + "");
                                break;
                            case ColorType.COLOR_RED:
                                holder.tv_left_name.setText(bean.teamName);
                                holder.tv_left_people_num.setText(bean.participantsNumber + "");
                                break;
                        }
                    }
                }
            } else {
                holder.ll_company.setVisibility(View.GONE);
                holder.ll_objective.setVisibility(View.VISIBLE);
                ((BannerViewHolder1) holder).tv_desc.setText(data.activityName);
                setSportType(((BannerViewHolder1) holder).tv_sports_type, data);
                String mileage = "目标：%skm";
                ((BannerViewHolder1) holder).tv_mileage.setText(String.format(mileage, data.targetMileage));
                String gift = "x%s";
                ((BannerViewHolder1) holder).tv_gift_num.setText(String.format(gift, data.prizeNumber));
                ((BannerViewHolder1) holder).tv_huo_num.setText(data.fireNumber + "");
            }
        }
//        else if (holder instanceof BannerViewHolder2) {
//            String time = "%s-%s";
//            ((BannerViewHolder2) holder).tv_time.setText(String.format(time, data.beginTime, data.deadlineTime));
//            setStatus(((BannerViewHolder2) holder).tv_status, data.activityStatus);
//            ((BannerViewHolder2) holder).tv_user_nick.setText(data.publisherName);
//            ((BannerViewHolder2) holder).tv_tenant_name.setText("-" + data.tenantName);
//            GlideUtils.loadImage(context, data.publisherheadImgUrl, ((BannerViewHolder2) holder).rci_user_header);
//
//            if (data.teamActivitiesDtoList != null && data.teamActivitiesDtoList.size() > 0) {
//                for (MyOngoingActivityListsBean.TeamActivitiesDtoListBean bean : data.teamActivitiesDtoList) {
//                    switch (bean.colourType) {
//                        case ColorType.COLOR_BLUE:
//                            ((BannerViewHolder2) holder).tv_right_name.setText(bean.teamName);
//                            ((BannerViewHolder2) holder).tv_right_people_num.setText(bean.participantsNumber + "");
//                            break;
//                        case ColorType.COLOR_RED:
//                            ((BannerViewHolder2) holder).tv_left_name.setText(bean.teamName);
//                            ((BannerViewHolder2) holder).tv_left_people_num.setText(bean.participantsNumber + "");
//                            break;
//                    }
//                }
//            }
//        }
    }

    private void setStatus(TextView view, int status) {
        switch (status) {
            case ActivityStatus.UNSTART:
                view.setText("报名中");
                view.setTextColor(context.getResources().getColor(R.color.color_FFF63E5A));
                view.setBackgroundResource(R.drawable.bg_ffeeee_4dp);
                break;
            case ActivityStatus.UNDER_WAY:
                view.setText("正在进行");
                view.setTextColor(context.getResources().getColor(R.color.color_ff7400));
                view.setBackgroundResource(R.drawable.bg_fff9f1_4dp);
                break;
            case ActivityStatus.FINISHED:
                view.setText("已结束");
                view.setTextColor(context.getResources().getColor(R.color.color_FF666666));
                view.setBackgroundResource(R.drawable.bg_fff4f4f4_cr_4dp);
                break;
        }
    }

    private void setSportType(TextView view, MyOngoingActivityListsBean data) {
        String str = "%s %s天";
        Drawable drawableLeft = null;
        switch (data.sportType) {
            case SportType.PAO_BU:
                view.setText(String.format(str, "跑步", data.hitCardChallengeDays));
                drawableLeft = context.getResources().getDrawable(
                        R.mipmap.ico_paobu);
                view.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
                view.setTextColor(context.getResources().getColor(R.color.color_285bc2));
                view.setBackgroundResource(R.drawable.bg_e4edff_4dp);
                break;
            case SportType.QI_XING:
                view.setText(String.format(str, "骑行", data.hitCardChallengeDays));
                drawableLeft = context.getResources().getDrawable(
                        R.mipmap.ico_qixing);
                view.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
                view.setTextColor(context.getResources().getColor(R.color.color_39b222));
                view.setBackgroundResource(R.drawable.bg_f2fcf0_4dp);
                break;
            case SportType.JIAN_ZOU:
                view.setText(String.format(str, "健走", data.hitCardChallengeDays));
                drawableLeft = context.getResources().getDrawable(
                        R.mipmap.ico_jianzou);
                view.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
                view.setTextColor(context.getResources().getColor(R.color.color_FFF63E5A));
                view.setBackgroundResource(R.drawable.bg_ffeeee_4dp);
                break;
        }
    }

    public class BannerViewHolder1 extends RecyclerView.ViewHolder {
        public TextView tv_time, tv_status, tv_user_nick, tv_tenant_name;
        public RoundedCornersImage rci_user_header;
        public TextView tv_desc, tv_sports_type, tv_mileage, tv_gift_num, tv_huo_num;

        public TextView tv_left_people_num, tv_left_name, tv_right_people_num, tv_right_name;

        public LinearLayout ll_company, ll_objective;

        public BannerViewHolder1(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_user_nick = itemView.findViewById(R.id.tv_user_nick);
            tv_tenant_name = itemView.findViewById(R.id.tv_tenant_name);
            rci_user_header = itemView.findViewById(R.id.rci_user_header);

            tv_desc = itemView.findViewById(R.id.tv_desc);
            tv_sports_type = itemView.findViewById(R.id.tv_sports_type);
            tv_mileage = itemView.findViewById(R.id.tv_mileage);
            tv_gift_num = itemView.findViewById(R.id.tv_gift_num);
            tv_huo_num = itemView.findViewById(R.id.tv_huo_num);

            tv_left_people_num = itemView.findViewById(R.id.tv_left_people_num);
            tv_left_name = itemView.findViewById(R.id.tv_left_name);
            tv_right_people_num = itemView.findViewById(R.id.tv_right_people_num);
            tv_right_name = itemView.findViewById(R.id.tv_right_name);


            ll_company = itemView.findViewById(R.id.ll_company);
            ll_objective = itemView.findViewById(R.id.ll_objective);
        }
    }

    public class BannerViewHolder2 extends RecyclerView.ViewHolder {
        public TextView tv_time, tv_status, tv_user_nick, tv_tenant_name;
        public RoundedCornersImage rci_user_header;
        public TextView tv_left_people_num, tv_left_name, tv_right_people_num, tv_right_name;

        public BannerViewHolder2(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_user_nick = itemView.findViewById(R.id.tv_user_nick);
            tv_tenant_name = itemView.findViewById(R.id.tv_tenant_name);
            rci_user_header = itemView.findViewById(R.id.rci_user_header);

            tv_left_people_num = itemView.findViewById(R.id.tv_left_people_num);
            tv_left_name = itemView.findViewById(R.id.tv_left_name);
            tv_right_people_num = itemView.findViewById(R.id.tv_right_people_num);
            tv_right_name = itemView.findViewById(R.id.tv_right_name);

        }
    }
}
